﻿using DGSFramework;
using System;
using System.Collections.Generic;
using System.Data.EntityClient;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Script.Serialization;
using System.Web.Services;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Framework
{
    public partial class MultipleChoice : System.Web.UI.Page
    {
        DGSFrameworkEntities entity = new DGSFrameworkEntities();
        List<udv_Question> allQuestion = new List<udv_Question>();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadCacheQuestion();
                LoadData();
                HttpCookie aCookie = new HttpCookie("lastVisit");
                aCookie.Value = DateTime.Now.ToString();
                aCookie.Expires = DateTime.Now.AddMinutes(30);
                Response.Cookies.Add(aCookie);
            }
        }

        public void LoadCacheQuestion()
        {

            allQuestion = entity.udv_Question.Include("udv_Answer").OrderByDescending(d => d.DisplayOrder).ToList();
        }
        public void LoadData()
        {
            rpQuestion.DataSource = allQuestion;
            rpQuestion.DataBind();
        }

        protected void rpQuestion_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.DataItem != null && e.Item.FindControl("rpAnswer") != null)
            {
                var question = (udv_Question)e.Item.DataItem;
                var rpAnswere = e.Item.FindControl("rpAnswer") as Repeater;
                rpAnswere.DataSource = question.udv_Answer.ToList();
                rpAnswere.DataBind();
            }
        }

        protected void rpAnswer_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.DataItem != null && e.Item.FindControl("ltAnswer") != null)
            {
                var answer = (udv_Answer)e.Item.DataItem;
                var ltAnswer = e.Item.FindControl("ltAnswer") as Literal;
                ltAnswer.Text = "<li><label><input type='radio' value='" + answer.udv_Question.ID + "_" + answer.ID + "' name='c_" + answer.udv_Question.ID + "' />" + answer.Answer + "</label></li>";
            }
        }

        [WebMethod]
        public static string CheckCorrect(string answerList, string fullName, string email, string phone, string job)
        {
            var lstCorrect = -1;
            try
            {
                var answerSplit = answerList.Split('|');
                var lstQuestionID = new List<string>();
                var lstanswerID = new List<string>();
                if (answerSplit.Count() > 0)
                {
                    foreach (var item in answerSplit)
                    {
                        var itemSplit = item.ToString().Split('_');
                        if (itemSplit.Count() == 2)
                        {
                            lstQuestionID.Add(itemSplit[0]);
                            lstanswerID.Add(itemSplit[1]);
                        }
                    }
                }

                DGSFrameworkEntities entity = new DGSFrameworkEntities();
                //lstCorrect = entity.udv_Answer.Where(d => lstQuestionID.Contains(d.udv_Question.ID) && lstanswerID.Contains(d.ID) && d.Correct == true).Count();
                var entityConn = entity.Connection as EntityConnection;
                var dbConn = entityConn.StoreConnection as SqlConnection;
                dbConn.Open();
                var sqlQuery = string.Format("SELECT COUNT (0) FROM udv_Answer WHERE QuestionID IN ({0}) AND id IN ({1}) AND correct=1", string.Join(",", lstQuestionID.ToArray()), string.Join(",", lstanswerID.ToArray()));
                var cmd = new SqlCommand(sqlQuery, dbConn);
                lstCorrect = (Int32)cmd.ExecuteScalar();                
                dbConn.Close();
                if (HttpContext.Current.Request.Cookies["lastVisit"] != null)
                {
                    var cus = new udv_QuestionInfo();
                    cus.FullName = fullName;
                    cus.Email = email;
                    cus.Phone = phone;
                    cus.Job = job;
                    cus.CorrectQuestion = lstCorrect;
                    cus.DateCreate = DateTime.Now;                    
                    entity.AddToudv_QuestionInfo(cus);
                    entity.SaveChanges();
                }
            }
            catch (Exception e)
            {


            }            
            return lstCorrect.ToString();
        }
    }
}